<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>libspotify: toplist.c</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />



</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">libspotify
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.8.0 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">toplist.c</div>  </div>
</div><!--header-->
<div class="contents">
<p>The toplist.c example shows how you can use toplist functions. It is part of the spshell program</p>
<div class="fragment"><pre class="fragment">
<span class="preprocessor">#include &lt;string.h&gt;</span>

<span class="preprocessor">#include &quot;spshell.h&quot;</span>
<span class="preprocessor">#include &quot;cmd.h&quot;</span>

<span class="keyword">static</span> <span class="keywordtype">void</span> print_album(<span class="keywordtype">int</span> index, <a class="code" href="group__types.html#ga6fd3e5de729c429fef3ff18ece6dcfad" title="An album handle.">sp_album</a> *album)
{
    printf(<span class="stringliteral">&quot;  Album %3d: \&quot;%s\&quot; by \&quot;%s\&quot;\n&quot;</span>, index, <a name="a0"></a><a class="code" href="group__album.html#ga0e828c991a8184d60026513f944e19ad">sp_album_name</a>(album), 
           <a name="a1"></a><a class="code" href="group__artist.html#gad2895555bb48514986823733a7afe5e2">sp_artist_name</a>(<a name="a2"></a><a class="code" href="group__album.html#ga42309972141dab10c32d741d357c01de">sp_album_artist</a>(album)));
}

<span class="keyword">static</span> <span class="keywordtype">void</span> print_artist(<span class="keywordtype">int</span> index, <a class="code" href="group__types.html#ga31764fbd69618c10be173c6c03d4c1bc" title="An artist handle.">sp_artist</a> *artist)
{
    <a class="code" href="group__types.html#gaf90a23d6b662c96d52ac19f7f4bc1e39" title="A handle to the libspotify internal representation of a URI.">sp_link</a> *l;
    <span class="keywordtype">char</span> url[200];
    printf(<span class="stringliteral">&quot;  Artist %3d: \&quot;%s\&quot;\n&quot;</span>, index, <a class="code" href="group__artist.html#gad2895555bb48514986823733a7afe5e2">sp_artist_name</a>(artist));

    l = <a name="a3"></a><a class="code" href="group__link.html#gaafb25ee14bfb27f9cbbab1b039d4d225">sp_link_create_from_artist_portrait</a>(artist, <a name="a4"></a><a class="code" href="group__session.html#gga61bc424465d776af3ceeb69eee2bb492ad20c808fd1f8180d1f49f643975d1dd7" title="Normal image size.">SP_IMAGE_SIZE_NORMAL</a>);
    <span class="keywordflow">if</span>(l != NULL) {
        <a name="a5"></a><a class="code" href="group__link.html#ga9f169be53f098adcf12f7a10b8bd3ef7">sp_link_as_string</a>(l, url, <span class="keyword">sizeof</span>(url));
        printf(<span class="stringliteral">&quot;    Portrait: %s\n&quot;</span>, url);
        <a name="a6"></a><a class="code" href="group__link.html#ga2b06ab16d1f6436cd8d36dbff2b5c022">sp_link_release</a>(l);
    }
}


<span class="keyword">static</span> <span class="keywordtype">void</span> SP_CALLCONV got_toplist(<a class="code" href="group__types.html#ga723de429cd073bcf5d13fb4e0f466ffd" title="A handle to a toplist browse result.">sp_toplistbrowse</a> *result, <span class="keywordtype">void</span> *userdata)
{
    <span class="keywordtype">int</span> i;

    <span class="comment">// We print from all types. Only one of the loops will acually yield anything.</span>

    <span class="keywordflow">for</span>(i = 0; i &lt; <a name="a7"></a><a class="code" href="group__toplist.html#gae2f5c09fbbf947834b1b1e73461f1fb3">sp_toplistbrowse_num_artists</a>(result); i++)
        print_artist(i + 1, <a name="a8"></a><a class="code" href="group__toplist.html#ga504cc45074d40c817dffbcc2906ef57f">sp_toplistbrowse_artist</a>(result, i));

    <span class="keywordflow">for</span>(i = 0; i &lt; <a name="a9"></a><a class="code" href="group__toplist.html#ga69f5e70269c1eb08e1ddb1c77bb8c3e3">sp_toplistbrowse_num_albums</a>(result); i++)
        print_album(i + 1, <a name="a10"></a><a class="code" href="group__toplist.html#gaa080ace445d269f56942ac3d7f1f157e">sp_toplistbrowse_album</a>(result, i));

    <span class="keywordflow">for</span>(i = 0; i &lt; <a name="a11"></a><a class="code" href="group__toplist.html#gaeb0ddfd565474dc7e444aabebbc119b7">sp_toplistbrowse_num_tracks</a>(result); i++) {
        printf(<span class="stringliteral">&quot;%3d: &quot;</span>, i + 1);
        print_track(<a name="a12"></a><a class="code" href="group__toplist.html#gaf3d00b8bf089134a4f320a8d3d6ab683">sp_toplistbrowse_track</a>(result, i));
    }

    <a name="a13"></a><a class="code" href="group__toplist.html#gac6cf969c9cb6b431aeaa4723820ca8de">sp_toplistbrowse_release</a>(result);
    cmd_done();
}



<span class="keyword">static</span> <span class="keywordtype">void</span> toplist_usage(<span class="keywordtype">void</span>)
{
    fprintf(stderr, <span class="stringliteral">&quot;Usage: toplist (tracks | albums | artists) (global | region &lt;countrycode&gt; | user)\n&quot;</span>);
}

<span class="keywordtype">int</span> cmd_toplist(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
{
    <a class="code" href="group__toplist.html#ga220f32522efb2828e4f8ffb949af234a">sp_toplisttype</a> type;
    <a class="code" href="group__toplist.html#ga6fa5cfed2cbb33f2f64542d0b63f6c8d">sp_toplistregion</a> region;

    <span class="keywordflow">if</span>(argc &lt; 3) {
        toplist_usage();
        <span class="keywordflow">return</span> -1;
    }

    <span class="keywordflow">if</span>(!strcasecmp(argv[1], <span class="stringliteral">&quot;artists&quot;</span>))
        type = <a name="a14"></a><a class="code" href="group__toplist.html#gga220f32522efb2828e4f8ffb949af234aa41523e58badf9e3901beea3f53e5d28e" title="Top artists.">SP_TOPLIST_TYPE_ARTISTS</a>;
    <span class="keywordflow">else</span> <span class="keywordflow">if</span>(!strcasecmp(argv[1], <span class="stringliteral">&quot;albums&quot;</span>))
        type = <a name="a15"></a><a class="code" href="group__toplist.html#gga220f32522efb2828e4f8ffb949af234aae8002818ef10eb2c75ec2058ce11cf6b" title="Top albums.">SP_TOPLIST_TYPE_ALBUMS</a>;
    <span class="keywordflow">else</span> <span class="keywordflow">if</span>(!strcasecmp(argv[1], <span class="stringliteral">&quot;tracks&quot;</span>))
        type = <a name="a16"></a><a class="code" href="group__toplist.html#gga220f32522efb2828e4f8ffb949af234aa6a85f8384ec77cb9cb5746966b0f0e28" title="Top tracks.">SP_TOPLIST_TYPE_TRACKS</a>;
    <span class="keywordflow">else</span> {
        toplist_usage();
        <span class="keywordflow">return</span> -1;
    }


    <span class="keywordflow">if</span>(!strcasecmp(argv[2], <span class="stringliteral">&quot;global&quot;</span>))
        region = <a name="a17"></a><a class="code" href="group__toplist.html#gga6fa5cfed2cbb33f2f64542d0b63f6c8da9e9136541401041c7d5c37708b7a927f" title="Global toplist.">SP_TOPLIST_REGION_EVERYWHERE</a>;
    <span class="keywordflow">else</span> <span class="keywordflow">if</span>(!strcasecmp(argv[2], <span class="stringliteral">&quot;user&quot;</span>))
        region = <a name="a18"></a><a class="code" href="group__toplist.html#gga6fa5cfed2cbb33f2f64542d0b63f6c8da6912eff58c71f5c74b78f48af5305375" title="Toplist for a given user.">SP_TOPLIST_REGION_USER</a>;
    <span class="keywordflow">else</span> <span class="keywordflow">if</span>(!strcasecmp(argv[2], <span class="stringliteral">&quot;region&quot;</span>)) {

        <span class="keywordflow">if</span>(argc != 4 || strlen(argv[3]) != 2) {
            toplist_usage();
            <span class="keywordflow">return</span> -1;
        }
        region = <a name="a19"></a><a class="code" href="group__toplist.html#gaa078b44491f976af43e4941fd1dd032c">SP_TOPLIST_REGION</a>(argv[3][0], argv[3][1]);
    } <span class="keywordflow">else</span> {
        toplist_usage();
        <span class="keywordflow">return</span> -1;
    }

    <a name="a20"></a><a class="code" href="group__toplist.html#gac2fe366e0050747438e09cdd22ed0e31">sp_toplistbrowse_create</a>(g_session, type, region, NULL, got_toplist, NULL);
    <span class="keywordflow">return</span> 0;
}
</pre></div> </div><!-- contents -->
<hr size="1"><address style="text-align: right;"><small>
Generated on Wed May 23 2012 11:04:07.<br>
Copyright &copy; 2006&ndash;2012 Spotify Ltd</small></address>
</body>
</html>
